﻿package zf.display.tools
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	//import fl.transitions.*;

	/**
	 * 工具提示隐藏时触发
	 * @eventType zf.display.tools.ToolTipEvent.HIDE
	 */
	[Bindable]
	[Event(name="hide", type="zf.display.tools.ToolTipEvent")]

	/**
	 * 工具提示显示时触发
	 * @eventType zf.display.tools.ToolTipEvent.SHOW
	 */
	[Bindable]
	[Event(name="show", type="zf.display.tools.ToolTipEvent")]

	/**
	 * 工具提示移动时触发
	 * @eventType zf.display.tools.ToolTipEvent.MOVE
	 */
	[Bindable]
	[Event(name="move", type="zf.display.tools.ToolTipEvent")]

	/**
	 * 工具提示。使用该类可在显示元件上添加一个工具提示，用来说明该元件。 
	 * @author mike.zhang
	 * @langversion 3.0 
	 * @playerversion Flash 9 
	 */
	public class ToolTip extends Sprite
	{
		public static  const TOOLTIP_UP:String="up";
		public static  const TOOLTIP_DOWN:String="down";
		public static  const TOOLTIP_LEFT:String="left";
		public static  const TOOLTIP_RIGHT:String="right";

		private var _txt:TextField;
		private var _bg:Sprite;
		private var _text:String;
		private var _tg:DisplayObject;
		private var _showtime:uint=3*1000;
		private var _timeBool:Boolean=true;
		private var _showTimer:Timer;
		private var _fx:String=TOOLTIP_UP;

		private var _bgcolor:uint=0xFEF3CD;
		private var _bgalpha:Number=1;
		private var _cc:Number=2;

		private var _followMouse:Boolean=true;
		
		private var _multiline:Boolean;

		

		/**
		 * 工具提示 
		 * @param target 要添加工具提示的 DisplayObject
		 * @param ss 说明性文字
		 * @param isMultiline 是否多行
		 * @param cc 提示框边缘圆角像素数，越大边缘越接近半圆。如果为0则边缘为直角
		 * @param bgcolor 背景颜色0xFEF3CD
		 * @param bgalpha 背景透明度，0-1之间
		 * 
		 */
		public function ToolTip (target:DisplayObject,ss:String=null,isMultiline:Boolean=false,cc:Number=2,bgcolor:uint=0xEDEDED,bgalpha:Number=1):void
		{
			_tg=target;
			_text=ss;
			_bgcolor=bgcolor;
			_bgalpha=bgalpha;
			_cc=cc;
			_multiline=isMultiline;
			if (_tg&&_text)
			{
				DrawTxt ();
				DrawBg (_txt.width,_txt.height,_cc,_bgcolor,_bgalpha);
				addEvent ();

				addChild (_bg);
				addChild (_txt);
				visible=false;
				_showTimer=new Timer(_showtime,1);
				_showTimer.addEventListener (TimerEvent.TIMER_COMPLETE,Tooltip_hideTooltip);
			}
		}
		private function addEvent ():void
		{
			_tg.addEventListener (MouseEvent.MOUSE_OVER,Tooltip_showTooltip);
			_tg.addEventListener (MouseEvent.MOUSE_OUT,Tooltip_hideTooltip);
			_tg.addEventListener (MouseEvent.MOUSE_MOVE,Tooltip_moveTooltip);
		}
		private function DrawBg (ww:Number,hh:Number,cc:Number=2,color:uint=0xFEF3CD,al:Number=1):void
		{
			try
			{
				removeChild (_bg);
			}
			catch (e:Error)
			{
			}
			_bg=new Sprite;
			_bg.graphics.beginFill (color,al);
			_bg.graphics.drawRoundRect (0,0,ww,hh,cc,cc);
			_bg.graphics.endFill ();
			var myFilter:DropShadowFilter = new DropShadowFilter (2, 45, 0x000000, 1, 4, 4, 1,1, false, false,false);
			var myFilters:Array = new Array();
			myFilters.push (myFilter);
			_bg.filters=myFilters;
		}
		private function DrawTxt ():void
		{
			_txt=new TextField()
			_txt.autoSize=TextFieldAutoSize.LEFT;
			//_txt.background=true;
			//_txt.border=true;
			//_txt.borderColor=0x000000;
			//_txt.backgroundColor=0xFEF3CD;
			_txt.selectable=false;
			_txt.text=" "+_text+" ";
			if(_multiline)
			{
				_txt.multiline=true;
				_txt.wordWrap=true;
				var mj:Number=_txt.textWidth*_txt.textHeight;
				var av:Number=Math.sqrt(mj/7);
				_txt.width=av*4;
			}

		}
		/**
		 * 依据方向设置工具提示的位置 
		 * 
		 */
		private function setLocation ():void
		{
			//_txt.x=mouseX-_txt.width/2;
			if (_fx==TOOLTIP_UP)
			{
				_txt.y=mouseY-5-_txt.height;
				_txt.x=mouseX-_txt.width/2;
			}
			else if (_fx==TOOLTIP_DOWN)
			{
				_txt.y=mouseY+20;
				_txt.x=mouseX-_txt.width/2;
			}
			else if (_fx==TOOLTIP_LEFT)
			{
				_txt.x=mouseX-_txt.width-10;
				_txt.y=mouseY-_txt.height/2;

			}
			else if (_fx==TOOLTIP_RIGHT)
			{
				_txt.x=mouseX+10;
				_txt.y=mouseY-_txt.height/2;

			}
			_bg.x=_txt.x;
			_bg.y=_txt.y;
		}
		private function Tooltip_showTooltip (e:MouseEvent=null):void
		{
			//trace(e.currentTarget);
			setLocation ();
			visible=true;
			if (_showtime>0 && _timeBool && _showTimer.running==false)
			{
				_showTimer.start();
			}
			this.dispatchEvent (new ToolTipEvent(ToolTipEvent.SHOW));
		}
		private function Tooltip_moveTooltip (e:MouseEvent=null):void
		{
			//trace(e.currentTarget);
			if (_followMouse)
			{
				setLocation ();
				e.updateAfterEvent ();
				if (_showtime>0 && _timeBool)
				{
					_timeBool=false;
					if (_showTimer.running)
					{
						_showTimer.reset ();
					}
					else
					{
						_showTimer.start ();
					}
				}
				this.dispatchEvent (new ToolTipEvent(ToolTipEvent.MOVE));
			}
		}
		private function Tooltip_hideTooltip (e:Event=null):void
		{
			visible=false;
			_showTimer.stop ();
			_showTimer.reset ();
			_timeBool=true;
			this.dispatchEvent (new ToolTipEvent(ToolTipEvent.HIDE));
		}

		/**
		 * 设置显示时间，单位毫秒，默认显示3000毫秒,设置为0时将在触发时永远显示
		 * @param num
		 * 
		 */
		public function set time (num:uint):void
		{
			_showtime=uint(num);
			if (_showtime>0)
			{
				_showTimer.delay=_showtime;
			}
		}
		public function get time ():uint
		{
			return _showtime;
		}
		/**
		 * 设置工具提示的出现方向，只能为Tooltip.TOOLTIP_UP,Tooltip.TOOLTIP_DOWN,TOOLTIP_LEFT或TOOLTIP_RIGHT。默认在上面 Tooltip.TOOLTIP_UP
		 * @param ss
		 * 
		 */
		public function set aspect (ss:String):void
		{
			if (ss==TOOLTIP_UP||ss==TOOLTIP_DOWN||ss==TOOLTIP_LEFT||ss==TOOLTIP_RIGHT)
			{
				_fx=ss;
			}
			else
			{
				throw new Error("Tooltip.size只能为Tooltip.TOOLTIP_UP,Tooltip.TOOLTIP_DOWN,TOOLTIP_LEFT或TOOLTIP_RIGHT");
			}
		}
		public function get aspect ():String
		{
			return _fx;
		}
		/**
		 * 获取工具提示的文本对象 
		 * @return TextField 文本
		 * 
		 */
		public function get textField ():TextField
		{
			return _txt;
		}
		/**
		 * 跟随鼠标，设置为true将跟随鼠标移动，否则不跟随 
		 * @param bool
		 * 
		 */
		public function set followMouse (bool:Boolean):void
		{
			_followMouse=bool;
		}
		public function get followMouse ():Boolean
		{
			return _followMouse;
		}
		/**
		 * 【只读】是否多行 
		 * @return 
		 * 
		 */
		public function get multiline():Boolean
		{
			return _multiline;
		}
		
		/*
		public function setTextFormat (format:TextFormat, beginIndex:int = -1, endIndex:int = -1)
		{
		_txt.setTextFormat (format, beginIndex, endIndex);
		DrawBg (_txt.width,_txt.height,_cc,_bgcolor,_bgalpha);
		addChild (_bg);
		if (getChildIndex(_bg)>getChildIndex(_txt))
		{
		swapChildren (_bg,_txt);
		}
		}
		*/
	}
}